package com.itheima.controller;

import com.itheima.pojo.Assignment;
import com.itheima.pojo.NoWorkStudent;
import com.itheima.pojo.User;
import com.itheima.pojo.UserAndWork;
import com.itheima.pojo.dto.NoWorkDTO;
import com.itheima.pojo.dto.WorkDTO;
import com.itheima.service.WorkService;
import com.itheima.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/work")
public class WorkController {

    @Autowired
    private WorkService workService;

    @PostMapping("/publishWork")
    public ResponseResult addWork(@RequestBody Assignment assignment, HttpSession session) throws ParseException {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String stringDate = sdf1.format(assignment.getCreatedOn());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date createdOn = sdf.parse(stringDate);
        assignment.setCreatedOn(createdOn);

        Object loginUser = session.getAttribute("user");
        if (loginUser != null) {
            User user = (User) loginUser;
            assignment.setUserId(user.getId());
        }
        boolean flag = workService.save(assignment);
        if (flag) {
            return new ResponseResult(0, "操作成功", "作业发布成功");
        }
        return new ResponseResult(1, "操作失败", "作业发布失败");
    }

    @PostMapping("/showWorks")
    public ResponseResult findAllWork(@RequestBody WorkDTO workDTO) throws ParseException {
        //转换时间格式
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String stringDate = sdf1.format(workDTO.getCreatedOn());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date createdOn = sdf.parse(stringDate);
        workDTO.setCreatedOn(createdOn);

        List<UserAndWork> users = workService.selectWorkByTypeAndTime(workDTO);
        if (users != null) {
            return new ResponseResult(0, "操作成功", users);
        }
        return new ResponseResult(1, "操作失败", "");
    }

    /**
     * 查询时间段未提交作业的学生以及次数
     */
    @PostMapping("/findNotWorkByTime")
    public ResponseResult findNotWorkByTime(@RequestBody NoWorkDTO noWorkDTO) throws ParseException {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String stringStart = sdf1.format(noWorkDTO.getStartTime());
        String stringEnd = sdf1.format(noWorkDTO.getEndTime());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = sdf.parse(stringStart);
        Date endTime = sdf.parse(stringEnd);
        noWorkDTO.setStartTime(startTime);
        noWorkDTO.setEndTime(endTime);

        List<NoWorkStudent> students = workService.findNotWorkByTime(noWorkDTO);
        if (students != null) {
            return new ResponseResult(0, "操作成功", students);
        }
        return new ResponseResult(1, "操作失败", "");
    }

}
